// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.gameScene = ck.getGameScene()
    },

    update (dt) {
        if (this.gameScene.data.state === ck.GameState.Prepare)
            return;
        if (this.gameScene.data.state === ck.GameState.TouchDoor)
            return;
        if (this.gameScene.data.state === ck.GameState.Finished)
            return;
        if (this.gameScene.data.state === ck.GameState.Dead)
            return
        if (!this.gameScene.player)
            return;

        var local = ck.localToLocalSpace(this.gameScene.player.node, this.node.parent)

        var left_bottom = this.gameScene.border.getLeftBottomRelativeNodeParent(this.node)
        var right_bottom = this.gameScene.border.getRightBottomRelativeNodeParent(this.node)
        if (local.x-cc.winSize.width/2 < left_bottom.x) {
            local.x = left_bottom.x+cc.winSize.width/2
        }
        if (local.y-cc.winSize.height/2 < left_bottom.y) {
            local.y = left_bottom.y+cc.winSize.height/2
        }

        if (local.x+cc.winSize.width/2 > right_bottom.x) {
            local.x = right_bottom.x-cc.winSize.width/2
        }

        this.node.position = local

    },
});
